# 各模型对比
import numpy as np
import pandas as pd
import os
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.preprocessing import scale, StandardScaler
from sklearn.model_selection import train_test_split, GridSearchCV, cross_val_score
from sklearn.metrics import confusion_matrix, accuracy_score, mean_squared_error, r2_score, roc_auc_score, roc_curve, classification_report
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.neural_network import MLPClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import GradientBoostingClassifier
from lightgbm import LGBMClassifier
from sklearn.model_selection import KFold

from sklearn.metrics import f1_score,precision_score,recall_score,roc_auc_score,accuracy_score,roc_curve
import matplotlib.pyplot as plt
from xgboost.sklearn import XGBClassifier
import lightgbm as lgb
import shap

data = pd.read_csv('../featureEngineering/featuredData.csv')
y = data['Outcome']
X = data.drop(['Outcome'],axis=1)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1234)

lr = LogisticRegression(random_state=12345)  # 逻辑回归模型
lr.fit(X_train, y_train)
lr_y_proba=lr.predict_proba(X_test)
lr_y_pre=lr.predict(X_test)

kr = KNeighborsClassifier()#KNN模型
kr.fit(X_train, y_train)
kr_y_proba=kr.predict_proba(X_test)
kr_y_pre=kr.predict(X_test)

svm = SVC(probability=True,gamma='auto', random_state = 12345)  # SVM模型
svm.fit(X_train, y_train)
svm_y_pre=svm.predict(X_test)
svm_y_proba=svm.predict_proba(X_test)

forest=RandomForestClassifier(random_state = 12345) #　随机森林
forest.fit(X_train,y_train)
forest_y_pre=forest.predict(X_test)
forest_y_proba=forest.predict_proba(X_test)


tr = DecisionTreeClassifier(random_state = 12345) # 决策树模型
tr.fit(X_train, y_train)
tr_y_pre=tr.predict(X_test)
tr_y_proba=tr.predict_proba(X_test)


Gbdt=GradientBoostingClassifier(random_state = 12345)  #CBDT
Gbdt.fit(X_train,y_train)
Gbdt_y_pre=Gbdt.predict(X_test)
Gbdt_y_proba=Gbdt.predict_proba(X_test)

# 模型评分
lr_score = lr.score(X_test, y_test)
lr_accuracy_score = accuracy_score(y_test,lr_y_pre)
lr_preci_score = precision_score(y_test,lr_y_pre)
lr_recall_score = recall_score(y_test,lr_y_pre)
lr_f1_score = f1_score(y_test,lr_y_pre)
lr_auc = roc_auc_score(y_test,lr_y_proba[:,1])
print('lr_accuracy_score: %f,lr_preci_score: %f,lr_recall_score: %f,lr_f1_score: %f,lr_auc: %f'
      %(lr_accuracy_score,lr_preci_score,lr_recall_score,lr_f1_score,lr_auc))


kr_score = kr.score(X_test, y_test)
kr_accuracy_score = accuracy_score(y_test,kr_y_pre)
kr_preci_score = precision_score(y_test,kr_y_pre)
kr_recall_score = recall_score(y_test,kr_y_pre)
kr_f1_score = f1_score(y_test,kr_y_pre)
kr_auc = roc_auc_score(y_test,kr_y_proba[:,1])
print('kr_accuracy_score: %f,kr_preci_score: %f,kr_recall_score: %f,kr_f1_score: %f,kr_auc: %f'
      %(kr_accuracy_score,kr_preci_score,kr_recall_score,kr_f1_score,kr_auc))

svm_accuracy_score = accuracy_score(y_test,svm_y_pre)
svm_preci_score = precision_score(y_test,svm_y_pre)
svm_recall_score = recall_score(y_test,svm_y_pre)
svm_f1_score = f1_score(y_test,svm_y_pre)
svm_auc = roc_auc_score(y_test,svm_y_proba[:,1])
print('svm_accuracy_score: %f,svm_preci_score: %f,svm_recall_score: %f,svm_f1_score: %f,svm_auc: %f'
      %(svm_accuracy_score,svm_preci_score,svm_recall_score,svm_f1_score,svm_auc))


forest_accuracy_score = accuracy_score(y_test,forest_y_pre)
forest_preci_score = precision_score(y_test,forest_y_pre)
forest_recall_score = recall_score(y_test,forest_y_pre)
forest_f1_score = f1_score(y_test,forest_y_pre)
forest_auc = roc_auc_score(y_test,forest_y_proba[:,1])
print('forest_accuracy_score: %f,forest_preci_score: %f,forest_recall_score: %f,forest_f1_score: %f,forest_auc: %f'
      %(forest_accuracy_score,forest_preci_score,forest_recall_score,forest_f1_score,forest_auc))

tr_score = tr.score(X_test, y_test)
tr_accuracy_score = accuracy_score(y_test,tr_y_pre)
tr_preci_score = precision_score(y_test,tr_y_pre)
tr_recall_score = recall_score(y_test,tr_y_pre)
tr_f1_score = f1_score(y_test,tr_y_pre)
tr_auc = roc_auc_score(y_test,tr_y_proba[:,1])
print('tr_accuracy_score: %f,tr_preci_score: %f,tr_recall_score: %f,tr_f1_score: %f,tr_auc: %f'
       %(tr_accuracy_score,tr_preci_score,tr_recall_score,tr_f1_score,tr_auc))



Gbdt_accuracy_score = accuracy_score(y_test,Gbdt_y_pre)
Gbdt_preci_score = precision_score(y_test,Gbdt_y_pre)
Gbdt_recall_score = recall_score(y_test,Gbdt_y_pre)
Gbdt_f1_score = f1_score(y_test,Gbdt_y_pre)
Gbdt_auc = roc_auc_score(y_test,Gbdt_y_proba[:,1])
print('Gbdt_accuracy_score: %f,Gbdt_preci_score: %f,Gbdt_recall_score: %f,Gbdt_f1_score: %f,Gbdt_auc: %f'
      %(Gbdt_accuracy_score,Gbdt_preci_score,Gbdt_recall_score,Gbdt_f1_score,Gbdt_auc))
